<template>
  <div>
    <div>
      <FormPanel ref="formPanel" :list="formList" :cols="1" :labelWidth="80" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="type !== 'show'" type="primary" @click="saveDialog">{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { getBookingLimitById, addBookingLimit, updateBookingLimit, getBookingType } from '@/api/repair/repairBusiness/appointmentLimit';

export default {
  name: 'addBookingLimit',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    changeHandle: Function,
    bookingLimitId: String,
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改' };
    this.saveData = null;
    return {
      formList: this.createFormList()
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  mounted() {
    console.log(this.type);
  },
  created() {
    this.init();
  },
  methods: {
    async init() {
      if (this.type === 'edit') {
        const res = await getBookingLimitById(this.bookingLimitId);
        this.formList.forEach(x => {
          x.initialValue = res.data[x.fieldName];
        });
        this.formList = [...this.formList];
      }
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '预约类别',
          fieldName: 'bookingTypeCode',
          placeholder: '请选择...',
          initialValue: '',
          // itemList: this.createDictList('1004'),
          request: {
            fetchApi: getBookingType,
            params: {},
            datekey: '',
            valueKey: 'BOOKING_TYPE_CODE',
            textKey: 'BOOKING_TYPE_NAME'
          },
          rules: [{ required: true, message: '请选择预约类别', trigger: 'blur' }]
        },
        {
          type: 'TIME_SELECT',
          label: '开始时间',
          fieldName: 'beginTime',
          placeholder: '请输入...',
          rules: [{ required: true, message: '请输入开始时间', trigger: 'blur' }]
        },
        {
          type: 'TIME_SELECT',
          label: '结束时间',
          unit: '元',
          fieldName: 'endTime',
          placeholder: '请输入...',
          rules: [{ required: true, message: '请输入结束时间', trigger: 'blur' }]
        },
        {
          type: 'INPUT_NUMBER',
          label: '饱和量',
          fieldName: 'saturation',
          placeholder: '请输入...',
          rules: [{ required: true, message: '请输入饱和量', trigger: 'change' }]
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let res = {};
        if (this.type === 'add') {
          res = await addBookingLimit(this.saveData);
          this.closeDialog('refresh');
          this.changeHandle();
        }
        if (this.type === 'edit') {
          // debugger;
          this.saveData.id = this.bookingLimitId;
          res = await updateBookingLimit(this.saveData);
          this.closeDialog('refresh');
          this.changeHandle();
        }

        // if (res.resultCode === 201) {

        //   this.closeDialog('refresh');
        // }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>

<style lang="less" scoped></style>
